package com.rq.viewtestapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.rq.viewtestapplication.manager.ActionManager;
import com.rq.viewtestapplication.manager.ChildManager;
import com.rq.viewtestapplication.manager.LinkManager;
import com.rq.viewtestapplication.manager.TouchManagerManager;


public class MindPanViewGroup extends FrameLayout implements ActionManager {
    TouchManagerManager touchManager = new TouchManagerManager();
    LinkManager linkManager = new LinkManager();
    ChildManager childManager = new ChildManager();
    private ActionManager mActionManager;

    Paint focusPaint;
    TextPaint debugPaint;

    public MindPanViewGroup(Context context) {
        this(context, null, 0);
    }

    public MindPanViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MindPanViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        touchManager.attachManager(childManager, linkManager);
        debugPaint = new TextPaint();
        debugPaint.setColor(Color.RED);
        debugPaint.setTextSize(24);
        focusPaint = new Paint();
        focusPaint.setColor(Color.RED);
        focusPaint.setStrokeWidth(10);
        focusPaint.setStyle(Paint.Style.STROKE);
        super.setWillNotDraw(false);
        this.mActionManager = this;
    }

    public ActionManager getActionManager() {
        return mActionManager;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (touchManager.dispatchTouchEvent(ev)) {
            return false;
        }
        return onTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        postInvalidate();
        return touchManager.dealTouch(ev);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (childManager.hasNoChild()) {
            childManager.init(this);
            linkManager.createLink(childManager.getChildInfo());
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String all = "(" + getX() + "," + getY() + ") w=" + getWidth() + ",h=" + getHeight() + "\n";
        all += childManager.getChildInfoStr();
        //从0,0开始绘制
        all += "\n" + touchManager.getActionTag();

        StaticLayout sl = new StaticLayout(all, debugPaint, getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
        canvas.translate(0, 0);
        sl.draw(canvas);
        linkManager.draw(canvas, focusPaint);
    }

    @Override
    public void onDrawForeground(@NonNull Canvas canvas) {
        super.onDrawForeground(canvas);
        touchManager.drawForeground(canvas, focusPaint);
    }

    @Override
    public void rollback() {
    }

    @Override
    public void addOne() {

    }
}
